<template>
  <div>
    <div class="title">
      <h3>分类管理</h3>
    </div>

    <div class="btnBox">
      <el-button type='primary' @click="dialogFormVisible = true">
        新增分类
      </el-button>
    </div>

    <el-table :data="tableData" border class="table" style="width: 920px; " lazy row-key="id"
      :tree-props="{ children: 'items', hasChildren: 'hasChildren' }">>
      <el-table-column prop="name" label="菜单名称" width="200" align="center">
      </el-table-column>

      <el-table-column prop="name" label="分类名称" width="200" align="center">
      </el-table-column>
      <el-table-column prop="sort" label="排序" align="center">
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" width="200" align="center">
      </el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button @click.native.prevent="deleteRow(scope.row.id)" type="text" size="small" style="color: red;">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加分类" :visible.sync="dialogFormVisible" @close="cancel">
      <el-form :model="form" label-width='90px'>
        <el-form-item label="分类名称">
          <el-input v-model="form.name" placeholder="请输入分类名称"></el-input>
        </el-form-item>

        <el-form-item label="分类层级">
          <el-input v-model="form.level" placeholder="请输入分类层级" @blur="handleLevel"></el-input>
        </el-form-item>
       
        <el-form-item label="父分类名称">
          <el-select v-model="form.parent_id" placeholder="请选择父分类名称" :disabled="isDisabled">
              <el-option v-for="item in parentArr" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
       
        <el-form-item label="分类排序">
          <el-input v-model="form.sort" placeholder="请输入分类排序"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false; onSubmit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import { getCategoryInfo, addCategory, delCategory } from '@/api/goods'


export default {
  name: 'productCategory',
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {},
      parentArr: [],
      isDisabled: false
    }
  },

  created() {
    this.getCategoryList()
  },

  activated() {
    this.getCategoryList()
  },

  methods: {

    getCategoryList() {
      getCategoryInfo({ page: 1, limit: 100 }).then((res) => {
        //console.log(res)
        this.tableData = res.data.list
       // console.log(this.tableData)
      })
    },

    deleteRow(id) {
      delCategory({ id }).then(res => {
        if (res.code == 1) {
          this.$message.success('删除成功')
          this.getCategoryList()
        }
      })
    },
    //递归获取每一级的所有对象
    flatten(arr){
      let res=[...arr]
      res.forEach(item=>{
        if(item.items instanceof Array){
        res= res.concat(this.flatten(item.items) )
        }
      }) 
      return res
    },
    
    handleLevel(){
      let arr=this.flatten(this.tableData)
      //console.log(arr)
      if(this.form.level>1){
        this.isDisabled=false
        this.parentArr=arr.filter(item=>{
        return item.level==this.form.level-1
      })
     // console.log(this.parentArr)
      }else{
        
        this.form.parent_id=this.form.name
        this.isDisabled=true
      }
      
    },


    onSubmit() {
      addCategory(this.form).then(res => {

        if (res.code == 1) {
          this.$message.success('添加成功')
          this.getCategoryList()
          this.cancel()
        }
      })
    },

    cancel() {
      this.form = {}
      this.parentArr = []
      this.isDisabled = false
    }

  },
 
}
</script>  
  
<style  scoped>
.title {
  padding: 20px;
  border-bottom: 1px solid #ccc;

}

.btnBox {
  padding: 20px;
}

.table {
  margin: 20px;
}
</style>
  